{% set pypi_name = 'cinder' %}
{% set source = fetch_source('https://tarballs.openstack.org/cinder/cinder-master.tar.gz') %}
{% set upstream_version = upstream_version() %}
{% set rpm_release = '1' %}
%global with_doc 1
%if 0%{?rhel} || 0%{?fedora}
%global rdo 1
%endif
%global service cinder
Name:           {{ py2name() }}
Version:        {{ py2rpmversion() }}
Release:        {{ py2rpmrelease() }}
Epoch:          {{ epoch('cinder') }}
Summary:        OpenStack Block Storage (Cinder)
License:        {{ license('Apache-2.0') }}
URL:            https://docs.openstack.org/cinder
Source0:        {{ source|basename }}
Source1:        openstack-cinder.logrotate
Source2:        openstack-cinder-api.service
Source3:        openstack-cinder-scheduler.service
Source4:        openstack-cinder-volume.service
Source5:        openstack-cinder-backup.service
Source6:        openstack-cinder.sudoers
Source7:        openstack-cinder.tmpfiles
Source8:        openstack-cinder.defaultconf
Source50:       openstack-cinder.README.config
BuildRequires:  openstack-macros
BuildRequires:  {{ py3('PyMySQL') }}
BuildRequires:  {{ py3('SQLAlchemy') }}
BuildRequires:  {{ py3('SQLAlchemy-Utils') }}
BuildRequires:  {{ py3('python-barbicanclient') }}
BuildRequires:  {{ py3('castellan') }}
BuildRequires:  {{ py3('cryptography') }}
BuildRequires:  {{ py3('cursive') }}
BuildRequires:  {{ py3('ddt') }}
BuildRequires:  {{ py3('decorator') }}
BuildRequires:  {{ py3('defusedxml') }}
BuildRequires:  {{ py3('fixtures') }}
BuildRequires:  {{ py3('python-glanceclient') }}
BuildRequires:  {{ py3('google-api-python-client') }}
BuildRequires:  {{ py3('keystonemiddleware') }}
BuildRequires:  {{ py3('lxml') }}
BuildRequires:  {{ py3('mock') }}
BuildRequires:  {{ py3('python-novaclient') }}
BuildRequires:  {{ py3('oauth2client') }}
BuildRequires:  {{ py3('os-brick') }}
BuildRequires:  {{ py3('os-win') }}
BuildRequires:  {{ py3('oslo.config') }}
BuildRequires:  {{ py3('oslo.db') }}
BuildRequires:  {{ py3('oslo.i18n') }}
BuildRequires:  {{ py3('oslo.log') }}
BuildRequires:  {{ py3('oslo.policy') }}
BuildRequires:  {{ py3('oslo.reports') }}
BuildRequires:  {{ py3('oslo.upgradecheck') }}
BuildRequires:  {{ py3('oslo.utils') }}
BuildRequires:  {{ py3('oslo.versionedobjects') }}
BuildRequires:  {{ py3('oslo.vmware') }}
BuildRequires:  {{ py3('oslotest') }}
BuildRequires:  {{ py3('osprofiler') }}
BuildRequires:  {{ py3('paramiko') }}
BuildRequires:  {{ py3('psycopg2') }}
BuildRequires:  {{ py3('pyparsing') }}
BuildRequires:  {{ py3('requests') }}
BuildRequires:  {{ py3('retrying') }}
BuildRequires:  {{ py3('rtslib-fb') }}
BuildRequires:  {{ py3('setuptools') }}
BuildRequires:  {{ py3('sqlalchemy-migrate') }}
BuildRequires:  {{ py3('suds-jurko') }}
BuildRequires:  {{ py3('python-swiftclient') }}
BuildRequires:  {{ py3('tabulate') }}
BuildRequires:  {{ py3('taskflow') }}
BuildRequires:  {{ py3('testresources') }}
BuildRequires:  {{ py3('testscenarios') }}
BuildRequires:  {{ py3('testtools') }}
BuildRequires:  {{ py3('tooz') }}
BuildRequires:  {{ py3('zstd') }}
BuildRequires:  sudo
Requires:       logrotate
Requires:       python3-{{ pypi_name }} = %{epoch}:%{version}-%{release}
BuildArch:      noarch
%if 0%{?suse_version}
BuildRequires:  systemd-rpm-macros
%{?systemd_requires}
%else
BuildRequires:  systemd
Requires(post): systemd
Requires(postun): systemd
Requires(preun): systemd
%endif
%if 0%{?suse_version}
Requires(pre):  pwdutils
%endif
%if 0%{?rdo}
Requires(pre):  shadow-utils
%endif

%description
OpenStack Volume (codename Cinder) provides services to manage and
access block storage volumes for use by Virtual Machine instances.

%package -n python3-{{ pypi_name }}
Summary:        OpenStack Volume Python libraries
Group:          Development/Languages/Python
Requires:       {{ py3('Paste') }}
Requires:       {{ py3('PasteDeploy') }}
Requires:       {{ py3('Routes') }}
Requires:       {{ py3('SQLAlchemy') }}
Requires:       {{ py3('WebOb') }}
Requires:       {{ py3('python-barbicanclient') }}
Requires:       {{ py3('castellan') }}
Requires:       {{ py3('cryptography') }}
Requires:       {{ py3('cursive') }}
Requires:       {{ py3('decorator') }}
Requires:       {{ py3('defusedxml') }}
Requires:       {{ py3('eventlet') }}
Requires:       {{ py3('python-glanceclient') }}
Requires:       {{ py3('google-api-python-client') }}
Requires:       {{ py3('httplib2') }}
Requires:       {{ py3('iso8601') }}
Requires:       {{ py3('jsonschema') }}
Requires:       {{ py3('keystoneauth1') }}
Requires:       {{ py3('python-keystoneclient') }}
Requires:       {{ py3('keystonemiddleware') }}
Requires:       {{ py3('lxml') }}
Requires:       {{ py3('python-novaclient') }}
Requires:       {{ py3('oauth2client') }}
Requires:       {{ py3('os-brick') }}
Requires:       {{ py3('os-win') }}
Requires:       {{ py3('oslo.concurrency') }}
Requires:       {{ py3('oslo.config') }}
Requires:       {{ py3('oslo.context') }}
Requires:       {{ py3('oslo.db') }}
Requires:       {{ py3('oslo.i18n') }}
Requires:       {{ py3('oslo.log') }}
Requires:       {{ py3('oslo.messaging') }}
Requires:       {{ py3('oslo.middleware') }}
Requires:       {{ py3('oslo.policy') }}
Requires:       {{ py3('oslo.privsep') }}
Requires:       {{ py3('oslo.reports') }}
Requires:       {{ py3('oslo.rootwrap') }}
Requires:       {{ py3('oslo.serialization') }}
Requires:       {{ py3('oslo.service') }}
Requires:       {{ py3('oslo.upgradecheck') }}
Requires:       {{ py3('oslo.utils') }}
Requires:       {{ py3('oslo.versionedobjects') }}
Requires:       {{ py3('oslo.vmware') }}
Requires:       {{ py3('osprofiler') }}
Requires:       {{ py3('paramiko') }}
Requires:       {{ py3('psutil') }}
Requires:       {{ py3('pyparsing') }}
Requires:       {{ py3('pytz') }}
Requires:       {{ py3('requests') }}
Requires:       {{ py3('retrying') }}
Requires:       {{ py3('rtslib-fb') }}
Requires:       {{ py3('simplejson') }}
Requires:       {{ py3('six') }}
Requires:       {{ py3('sqlalchemy-migrate') }}
Requires:       {{ py3('stevedore') }}
Requires:       {{ py3('suds-jurko') }}
Requires:       {{ py3('python-swiftclient') }}
Requires:       {{ py3('tabulate') }}
Requires:       {{ py3('taskflow') }}
Requires:       {{ py3('tooz') }}
Requires:       {{ py3('zstd') }}
Requires:       sudo

%description -n python3-{{ pypi_name }}
OpenStack Volume (codename Cinder) provides services to manage and
access block storage volumes for use by Virtual Machine instances.

This package contains the Cinder Python library.

%if 0%{?with_doc}
%package doc
Summary:        Documentation for OpenStack Volume Service
Group:          Documentation/HTML
BuildRequires:  {{ py3('Sphinx') }}
BuildRequires:  {{ py3('eventlet') }}
BuildRequires:  {{ py3('openstackdocstheme') }}
BuildRequires:  {{ py3('sphinx-feature-classification') }}
BuildRequires:  {{ py3('sphinxcontrib-apidoc') }}

%description doc
OpenStack Volume (codename Cinder) provides services to manage and
access block storage volumes for use by Virtual Machine instances.

This package contains the documentation.
%endif

%package api
Summary:        OpenStack Volume service (Cinder) - API
Group:          Development/Languages/Python
Requires:       python3-{{ pypi_name }} = %{epoch}:%{version}-%{release}

%description api
OpenStack Volume (codename Cinder) provides services to manage and
access block storage volumes for use by Virtual Machine instances.

This package contains the %{name} API server.

%package scheduler
Summary:        OpenStack Volume service (Cinder) - Scheduler
Group:          Development/Languages/Python
Requires:       %{name} = %{epoch}:%{version}-%{release}

%description scheduler
OpenStack Volume (codename Cinder) provides services to manage and
access block storage volumes for use by Virtual Machine instances.

This package contains the %{name} scheduler server.

%package volume
Summary:        OpenStack Volume service (Cinder) - Volume
Group:          Development/Languages/Python
Requires:       %{name} = %{epoch}:%{version}-%{release}
%if 0%{?suse_version}
Requires:       nfs-client
%else
Requires:       nfs-utils
%endif

%description volume
OpenStack Volume (codename Cinder) provides services to manage and
access block storage volumes for use by Virtual Machine instances.

This package contains the %{name} volume server.

%package backup
Summary:        OpenStack Volume service (Cinder) - Backup
Group:          Development/Languages/Python
Requires:       %{name} = %{epoch}:%{version}-%{release}

%description backup
OpenStack Volume (codename Cinder) provides services to manage and
access block storage volumes for use by Virtual Machine instances.

This package contains the %{name} backup server.

%prep
%autosetup -p1 -n {{ pypi_name }}-{{ upstream_version }}
%py_req_cleanup
sed -i 's/^warning-is-error.*/warning-is-error = 0/g' setup.cfg

# We do not want to depend on hacking for unit tests
rm -f cinder/tests/unit/test_hacking.py

%build
%{py3_build}
# build documentation
%if 0%{?with_doc}
export PYTHONPATH="$( pwd ):$PYTHONPATH"
PBR_VERSION={{ upstream_version }} %sphinx_build --keep-going -b html doc/source doc/build/html || :
PBR_VERSION={{ upstream_version }} %sphinx_build --keep-going -b man doc/source doc/build/man || :
rm -rf doc/build/html/.{doctrees,buildinfo}
%endif
# generate the sample config files
PYTHONPATH=. oslo-config-generator --config-file tools/config/cinder-config-generator.conf --output-file etc/cinder/cinder.conf
# generate the sample policy
PYTHONPATH=. oslopolicy-sample-generator --config-file=tools/config/cinder-policy-generator.conf --output etc/cinder/policy.json

%install
%{py3_install}

# Setup directories
install -d -m 750 %{buildroot}%{_localstatedir}/{lib,log,cache}/%{service}
install -d -m 755 %{buildroot}%{_sysconfdir}/%{service}
install -d -m 755 %{buildroot}%{_sysconfdir}/%{service}/%{service}.conf.d
install -d -m 755 %{buildroot}%{_sysconfdir}/%{service}/%{service}-volume.conf.d

# install tmpfiles
install -D -m 644 %{SOURCE7} %{buildroot}/%_tmpfilesdir/%name.conf

# install config files
install -d -m 755 %{buildroot}%{_sysconfdir}/%{service}
install -p -D -m 644 etc/%{service}/{cinder.conf,api-paste.ini,policy.json,resource_filters.json,rootwrap.conf} %{buildroot}%{_sysconfdir}/%{service}/
cp -a etc/cinder/rootwrap.d/ %{buildroot}%{_sysconfdir}/%{service}/
install -p -D -m 640 %{SOURCE50} %{buildroot}%{_sysconfdir}/%{service}/README.config
install -p -D -m 640 %{SOURCE8} %{buildroot}%{_sysconfdir}/cinder/cinder.conf.d/010-cinder.conf

# install systemd service files
install -p -D -m 644 %{SOURCE2} %{buildroot}%{_unitdir}/openstack-%{service}-api.service
install -p -D -m 644 %{SOURCE3} %{buildroot}%{_unitdir}/openstack-%{service}-scheduler.service
install -p -D -m 644 %{SOURCE4} %{buildroot}%{_unitdir}/openstack-%{service}-volume.service
install -p -D -m 644 %{SOURCE5} %{buildroot}%{_unitdir}/openstack-%{service}-backup.service

# install symlinks on SUSE
%if 0%{?suse_version}
mkdir -p %{buildroot}%{_sbindir}
ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}-api
ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}-scheduler
ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}-volume
ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}-backup
%endif

# Install sudoers
install -p -D -m 440 %{SOURCE6} %{buildroot}%{_sysconfdir}/sudoers.d/openstack-cinder

# Install logrotate
install -p -D -m 644 %{SOURCE1} %{buildroot}%{_sysconfdir}/logrotate.d/openstack-%{service}

# man pages
%if 0%{?with_doc}
mkdir -p %{buildroot}%{_mandir}/man1
install -p -D -m 644 doc/build/man/*.1 %{buildroot}%{_mandir}/man1/
%endif

# Remove duplicate config files under /usr/etc/
rm -rf %{buildroot}%{_prefix}/%{_sysconfdir}

%pre
%openstack_pre_user_group_create cinder cinder /sbin/nologin
exit 0

%post
%tmpfiles_create %{_tmpfilesdir}/%{name}.conf

%check
stestr run


%post api
%systemd_post %{name}-api.service

%preun api
%systemd_preun %{name}-api.service

%postun api
%systemd_postun_with_restart %{name}-api.service

%post scheduler
%systemd_post %{name}-scheduler.service

%preun scheduler
%systemd_preun %{name}-scheduler.service

%postun scheduler
%systemd_postun_with_restart %{name}-scheduler.service

%post volume
%systemd_post %{name}-volume.service

%preun volume
%systemd_preun %{name}-volume.service

%postun volume
%systemd_postun_with_restart %{name}-volume.service

%post backup
%systemd_post %{name}-backup.service

%preun backup
%systemd_preun %{name}-backup.service

%postun backup
%systemd_postun_with_restart %{name}-backup.service

%files
%license LICENSE
%dir %attr(0750, root, cinder) %{_sysconfdir}/{{ pypi_name }}
%dir %attr(0750, root, cinder) %{_sysconfdir}/{{ pypi_name }}/{{ pypi_name }}.conf.d/
%{_sysconfdir}/%{service}/README.config
%config(noreplace) %attr(-, root, %{service}) %{_sysconfdir}/%{service}/%{service}.conf
%config(noreplace) %attr(-, root, %{service}) %{_sysconfdir}/%{service}/api-paste.ini
%config(noreplace) %attr(-, root, %{service}) %{_sysconfdir}/%{service}/rootwrap.conf
%config(noreplace) %attr(0640, root, %{service}) %{_sysconfdir}/cinder/cinder.conf.d/010-cinder.conf
%config(noreplace) %{_sysconfdir}/logrotate.d/openstack-%{service}
%config(noreplace) %{_sysconfdir}/sudoers.d/openstack-%{service}
%config(noreplace) %{_sysconfdir}/%{service}/policy.json
%config(noreplace) %{_sysconfdir}/%{service}/resource_filters.json
%_tmpfilesdir/%name.conf
%dir %attr(0750, %{service}, root) %{_sysconfdir}/%{service}/rootwrap.d/
%dir %attr(0750, %{service}, root) %{_localstatedir}/log/%{service}
%dir %attr(0750, %{service}, root) %{_localstatedir}/lib/%{service}
%{_bindir}/{{ pypi_name }}-manage
%{_bindir}/{{ pypi_name }}-rootwrap
%{_bindir}/{{ pypi_name }}-rtstool
%{_bindir}/{{ pypi_name }}-volume-usage-audit
%{_bindir}/{{ pypi_name }}-status
%if 0%{?with_doc}
%{_mandir}/man1/cinder-manage.1.gz
%{_mandir}/man1/cinder-status.1.gz
%endif

%files -n python3-cinder
%license LICENSE
%{python3_sitelib}/{{ pypi_name }}*

%if 0%{?with_doc}
%files doc
%license LICENSE
%doc doc/build/html
%endif

%files api
%license LICENSE
%{_bindir}/{{ pypi_name }}-api
%{_bindir}/{{ pypi_name }}-wsgi
%{_unitdir}/openstack-cinder-api.service
%if 0%{?suse_version}
%{_sbindir}/rc%{name}-api
%endif

%files scheduler
%license LICENSE
%{_bindir}/{{ pypi_name }}-scheduler
%{_unitdir}/openstack-cinder-scheduler.service
%if 0%{?suse_version}
%{_sbindir}/rc%{name}-scheduler
%endif

%files volume
%license LICENSE
%dir %{_sysconfdir}/%{service}/%{service}-volume.conf.d/
%config %{_sysconfdir}/%{service}/rootwrap.d/volume.filters
%{_bindir}/{{ pypi_name }}-volume
%{_unitdir}/openstack-cinder-volume.service
%if 0%{?suse_version}
%{_sbindir}/rc%{name}-volume
%endif

%files backup
%license LICENSE
%{_bindir}/{{ pypi_name }}-backup
%{_unitdir}/openstack-cinder-backup.service
%if 0%{?suse_version}
%{_sbindir}/rc%{name}-backup
%endif

%changelog

